package mt.client;

import java.util.LinkedList;

import javax.swing.table.AbstractTableModel;

import mt.Order;

/**
 * "My Table Model" creates a list of all orders requested and takes care of the orders in the GUI.
 * 
 * @author Group17
 *
 */


public class MyTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 1L;
	private LinkedList<Order> orders = new LinkedList<Order>();
	
	/**
	 * Get the number of columns.
	 * Each column has its own information about one order.
	 * 
	 * @return  6 ( The 6 columns that appear in the GUI ).
	 */
	
	@Override
	public int getColumnCount() {
		return 6;
	}
	
	/**
	 * Get the number of rows
	 * 
	 * @return orders.size, the is the number of orders in the List "orders", that is equal to the number of
	 * 		rows occupied on the table.
	 */

	@Override
	public int getRowCount() {
		return orders.size();
	}
	
	/**
	 * Get the column names
	 * 
	 * @param int column -  there are 6 columns, the numbers 0 to 5 represents each column.
	 * 
	 * @return String "Nickname" case int column equals 0;
	 * @return String "Stock" case int column equals 1;
	 * @return String "Unit" case int column equals 2;
	 * @return String "Price/Unit" case int column equals 3;
	 * @return String "Type of order" case int column equals 4;
	 * @return String "Server ID" case int column equals 5;
	 * @return Null in all other cases
	 */

	@Override
	public String getColumnName(int column) {
		switch(column){
		case 0:
			return "Nickname";
		case 1:
			return "Stock";
		case 2:
			return "Units";
		case 3:
			return "Price/Unit";
		case 4:
			return "Type of order";
		case 5:
			return "Server ID";
		}
		return null;
	}

	/**
	 * To get the value at any position in the table
	 * 
	 * @param int rowIndex - to know the number of the row(order) we want to search, goes to the list
	 * 					of orders and chose the order number 'rowIndex'.
	 * @param int columnIndex - to get the value we want about that order: 
	 * 							0 - to get the nickname
	 * 							1 - to get the stock
	 * 							2 - to get the number of units
	 * 							3 - to get the price per unit
	 * 							4 - to get if the order is to buy or to sell
	 * 							5 - to get the server ID
	 * 
	 * @return orders.get(rowIndex).get...(). Returns the value of the 'order rowIndex' that is in the 
	 * 										'column columnIndex'. 
	 */
	
	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Order order = orders.get(rowIndex);
		switch(columnIndex){
		case 0:
			return order.getNickname();
		case 1:
			return order.getStock();
		case 2:
			return order.getNumberOfUnits();
		case 3:
			return order.getPricePerUnit();
		case 4:
			if(order.isBuyOrder())
			return "Buy";
			else return "Sell";
		case 5:
			return order.getServerOrderID();
		}
		return null;
	}
	
	/**
	 * Adds one order to the list 'orders'.
	 * 
	 * @param Order order - receives an order and adds it to the list of orders.
	 */

	public void addOrder(Order order) {
		orders.add(order);
		fireTableDataChanged();
	}
	
	/**
	 * Removes one order of the list 'orders'.
	 * 
	 * @param Order o - receives an order and compares it to all orders in the list 'orders'.
	 * 				When the right order is found it is deleted of the list 'orders'.
	 */
	
	public void removeOrder(Order o) {
		for (int i = 0; i < orders.size(); i++) {
			if(orders.get(i).getNickname().equals(o.getNickname()) &&
				orders.get(i).getStock().equals(o.getStock()) &&
				orders.get(i).getPricePerUnit()==o.getPricePerUnit() &&
				orders.get(i).getServerOrderID()==o.getServerOrderID()) {
					orders.remove(orders.get(i));
				}
		}
		System.out.println(orders.size());
		fireTableDataChanged();
	}
}
